package com.ffms.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Date;
import java.util.Arrays;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ffms.entity.Admin;

import com.ffms.entity.FamilyMember;
import com.ffms.service.impl.AdminServiceImpl;
import com.ffms.service.AdminService;
import com.sun.xml.internal.ws.api.message.MessageWritable;

@WebServlet("/UpdatePwdServlet")
public class UpdatePwdServlet extends HttpServlet {


    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        this.doPost(request, response);

    }


//
    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        //1.接收视图层的请求数据
        String userName = request.getParameter("username");
        String password = request.getParameter("password");
        String role = request.getParameter("role");
        int i = 0;
        AdminService adminService = new AdminServiceImpl();
        if (role.equals("admin")) {
            i = adminService.updateAd(new Admin(userName, password));
        } else if (role.equals("fm")) {
            i = adminService.updateFM(new FamilyMember(userName, password));
        }
        //根据结果跳转到不同的页面 成功：主界面 失败：原界面
        if (i > 0) {
            //成功跳转到主页面
            request.setAttribute("success", "密码修改成功！");
            request.getRequestDispatcher("/login.jsp").forward(request, response);

        } else {
            //失败跳回主界面
            request.setAttribute("error", "用户名不存在");
            request.getRequestDispatcher("/admin/updatePwd.jsp").forward(request, response);
        }


    }

}
